package com.zhengtoudai.ui.fragment.investment;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zhengtoudai.R;
import com.zhengtoudai.base.App;
import com.zhengtoudai.base.MvpFragment;
import com.zhengtoudai.ui.activity.InvestmentActivity;
import com.zhengtoudai.ui.activity.LoginActivity;
import com.zhengtoudai.ui.adapter.BorrowAdapter;
import com.zhengtoudai.ui.bean.BorrowModel;
import com.zhengtoudai.ui.presenter.BorrowPresenter;
import com.zhengtoudai.ui.view.BorrowView;
import com.zhengtoudai.util.AbToastUtil;
import com.zhengtoudai.util.Tool;
import com.zhengtoudai.view.recyclerview.RecyclerViewAdapter;
import com.zhengtoudai.view.swipetoloadlayout.base.OnLoadMoreListener;
import com.zhengtoudai.view.swipetoloadlayout.base.OnRefreshListener;
import com.zhengtoudai.view.swipetoloadlayout.base.SwipeToLoadLayout;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.zhengtoudai.R.id.refresh;

/**
 * 作者：黑哥 on 2016/8/29 13:37
 */
public class BorrowFragment extends MvpFragment<BorrowPresenter> implements BorrowView, OnRefreshListener, OnLoadMoreListener {
    @Bind(R.id.swipe_target)
    RecyclerView mSwipeTarget;
    @Bind(refresh)
    SwipeToLoadLayout mRefresh;
    @Bind(R.id.tv_default_sort)
    TextView mTvDefaultSort;
    @Bind(R.id.tv_the_cast_money)
    TextView mTvTheCastMoney;
    @Bind(R.id.tv_investment_time_limit)
    TextView mTvInvestmentTimeLimit;
    @Bind(R.id.tv_year_rate)
    TextView mTvYearRate;
    private BorrowAdapter mAdapter;

    private int pageNum;
    private int totalNum;
    private int CastMoneyNum = 0,InvestmentTimeLimitNum = 0,YearRateNum = 0; //点击的次数
    private boolean isThree = false; //是否点击了 3次

    //是否下拉刷新
    private boolean isPullDown;

    private String up_rate="";
    private String down_rate="";
    private String up_money="";
    private String down_money="";
    private String up_term="";
    private String down_term="";

    private Drawable drawable3;

    private FragmentActivity mActivity;

    public BorrowFragment() {


    }
    public void setRefreshEnabled(boolean enabled){
        if (mRefresh!=null){
            mRefresh.setRefreshEnabled(enabled);
        }
    }
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_project, container, false);
        mActivity = getActivity();
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initView() {

        mRefresh.setOnRefreshListener(this);
        mRefresh.setOnLoadMoreListener(this);

        drawable3 = ContextCompat.getDrawable(mContext,R.mipmap.sort3) ;
        drawable3.setBounds(0, 0, drawable3.getMinimumWidth(), drawable3.getMinimumHeight());

        mSwipeTarget.setLayoutManager(new LinearLayoutManager(activity));
        mAdapter = new BorrowAdapter(this);
        mSwipeTarget.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new RecyclerViewAdapter.OnItemClick() {
            @Override
            public void onItemClick(View view, int position) {
                if (App.mApp.hasLogin()){
                    Bundle bundle=new Bundle();
                    bundle.putString("borrowEId",mAdapter.getData().get(position).getBorrowEid());
                    bundle.putString("borrowStatus",mAdapter.getData().get(position).getBorrowStatus());
                    startActivity(bundle,InvestmentActivity.class);
                }else{
                    startActivity(LoginActivity.class);
                    AbToastUtil.showToast(mContext,"请先登录");
                }
            }
        });
        mRefresh.post(new Runnable() {
            @Override
            public void run() {
                mRefresh.setRefreshing(true);
            }
        });
        autoLoading(mRefresh, mSwipeTarget);
    }

    @Override
    protected BorrowPresenter createPresenter() {
        return new BorrowPresenter(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.tv_default_sort, R.id.tv_the_cast_money, R.id.tv_investment_time_limit, R.id.tv_year_rate})
    public void onClick(View view) {
        if (Tool.isFastDoubleClick()) return;
        switch (view.getId()) {
            case R.id.tv_default_sort:
                isPullDown=true;
                mTvTheCastMoney.setCompoundDrawables(null,null,drawable3,null);
                mTvInvestmentTimeLimit.setCompoundDrawables(null,null,drawable3,null);
                mTvYearRate.setCompoundDrawables(null,null,drawable3,null);
                senHttp(1,"","","","","","");
                break;
            //起投金额
            case R.id.tv_the_cast_money:
                mTvInvestmentTimeLimit.setCompoundDrawables(null,null,drawable3,null);
                mTvYearRate.setCompoundDrawables(null,null,drawable3,null);

                if (!isThree) {
                    CastMoneyNum++;
                    changeBgImage(CastMoneyNum, mTvTheCastMoney);
                }else{
                    CastMoneyNum=0;
                }

                break;
            //投资期限
            case R.id.tv_investment_time_limit:
                mTvTheCastMoney.setCompoundDrawables(null,null,drawable3,null);
                mTvYearRate.setCompoundDrawables(null,null,drawable3,null);
                if (!isThree) {
                    InvestmentTimeLimitNum++;
                    changeBgImage( InvestmentTimeLimitNum, mTvInvestmentTimeLimit);
                }else{
                    InvestmentTimeLimitNum=0;
                }

                break;
            //tv_year_rate
            case R.id.tv_year_rate:
                mTvTheCastMoney.setCompoundDrawables(null,null,drawable3,null);
                mTvInvestmentTimeLimit.setCompoundDrawables(null,null,drawable3,null);
                if (!isThree) {
                    YearRateNum++;
                    changeBgImage( YearRateNum, mTvYearRate);
                }else{
                    YearRateNum=0;
                }
                break;
        }
    }

    @Override
    public void showLoading() {

    }


    @Override
    public void getDataSuccess(BorrowModel model) {
        BorrowModel.BorrowBean mInfo = model.getResult();
        if (isPullDown){
            mAdapter.clearData();
        }

        totalNum = Integer.valueOf(mInfo.getTotalPage());
        pageNum = Integer.valueOf(mInfo.getPageNum());
        if (mInfo.getBorrowList() == null || mInfo.getBorrowList().size() == 0) {
            showNoData("暂无数据");
        } else {
            removeStatus();
            mAdapter.addData(mInfo.getBorrowList());
        }
    }

    @Override
    public void getDataFail(String msg) {

        if (mRefresh.isRefreshing()) {
            mRefresh.setRefreshing(false);
        } else {
            mRefresh.setLoadingMore(false);
        }
    }

    @Override
    public void hideLoading() {
        if (mRefresh.isRefreshing()) {
            mRefresh.setRefreshing(false);
        } else {
            mRefresh.setLoadingMore(false);
        }
    }

    @Override
    public void onLoadMore() {
        Log.e("LW","调用上拉加载");
        if (pageNum < totalNum) {
            isPullDown=false;
            int current = pageNum + 1;
            senHttp(current,"","","","","","");
        } else {
            mRefresh.setLoadingMore(false);
            AbToastUtil.showToast(mActivity, "已无更多数据");
        }

    }
    @Override
    public void onRefresh() {
        mTvTheCastMoney.setCompoundDrawables(null,null,drawable3,null);
        mTvInvestmentTimeLimit.setCompoundDrawables(null,null,drawable3,null);
        mTvYearRate.setCompoundDrawables(null,null,drawable3,null);
        /**
         *
         * 如果是下拉刷新 其他按钮的状态都改变
         *
         * */
        isPullDown=true;
        senHttp(1,"","","","","","");

    }

    private void senHttp(int current,String up_money,String down_money ,String up_term,String down_term,String up_rate,String down_rate) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("numPerPage", "10");
        map.put("pageNum", current + "");
        map.put("up_money", up_money);
        map.put("down_money", down_money);
        map.put("up_term", up_term);
        map.put("down_term", down_term);
        map.put("up_rate", up_rate);
        map.put("down_rate", down_rate);
        Log.e("LW","请求数据="+map.toString());
        mvpPresenter.loadData(mContext,map);
    }


    /**
     * 更改上下图标样式
     */
    private void changeBgImage(int number, TextView textView) {
        switch (number) {
            case 1:
                Drawable drawable1 = ContextCompat.getDrawable(mContext,R.mipmap.sort2) ;
                drawable1.setBounds(0, 0, drawable1.getMinimumWidth(), drawable1.getMinimumHeight());
                textView.setCompoundDrawables(null,null,drawable1,null);

                isPullDown=true;
                if(textView.equals(mTvTheCastMoney)){
                    up_money="up_money";
                }else{
                    up_money="";
                }
                if(textView.equals(mTvInvestmentTimeLimit)){
                    up_term="up_term";
                }else{
                    up_term="";
                }
                if(textView.equals(mTvYearRate)){
                    up_rate="up_rate";
                }else {
                    up_rate="";
                }
                Log.e("lw","b进入哪里了===1");
                senHttp(1,up_money,down_money,up_term,down_term,up_rate,down_rate);
                up_money="";
                up_term="";
                up_rate="";

                break;
            case 2:
                Drawable drawable2 = ContextCompat.getDrawable(mContext,R.mipmap.sort1) ;
                drawable2.setBounds(0, 0, drawable2.getMinimumWidth()-1, drawable2.getMinimumHeight()+1);
                textView.setCompoundDrawables(null,null,drawable2,null);
                isPullDown=true;
                if(textView.equals(mTvTheCastMoney)){
                    down_money="down_money";
                }else{
                    down_money="";
                }
                if(textView.equals(mTvInvestmentTimeLimit)){
                    down_term="down_term";
                }else{
                    down_term="";
                }
                if(textView.equals(mTvYearRate)){
                    down_rate="down_rate";
                }else {
                    down_rate="";
                }
                CastMoneyNum = 0;
                InvestmentTimeLimitNum=0;
                YearRateNum=0;
                Log.e("lw","b进入哪里了===2");
                senHttp(1,up_money,down_money,up_term,down_term,up_rate,down_rate);
                down_money="";
                down_term="";
                down_rate="";
                break;
//            case 3:
//                isPullDown=true;
//                Drawable drawable3 = ContextCompat.getDrawable(mContext,R.mipmap.sort3) ;
//                drawable3.setBounds(0, 0, drawable3.getMinimumWidth(), drawable3.getMinimumHeight());
//                textView.setCompoundDrawables(null,null,drawable3,null);
//                CastMoneyNum = 0;
//                InvestmentTimeLimitNum=0;
//                YearRateNum=0;
//                senHttp(1,"","","","","","");
//                break;
        }
    }


}
